#coding:utf-8
import functools
import time 
import logging
def log(fuc):
    #日志监控
    @functools.wraps(fuc)
    def wrapper(*args, **kwargs):
        print "%s fuc start ..." % fuc.__name__
        t1 = time.time()
        result = fuc(*args, **kwargs)
        t2 = time.time()
        time_Val = int(float( '%.3f' % (t2 - t1))*1000)
        print "%s fuc end ..." % fuc.__name__
        print "{1} fuc total running: {0}ms".format(str(time_Val), fuc.__name__)
        print "-"*16 + "Response" + "-"*16
        print result,"\n"
        return result
    return wrapper


def logs(totaltime=True):
    #函数执行时间统计
    def wrapper(func):
        @functools.wraps(func)
        def _wrapper(*args, **kwargs):
            print "%s func start ..." % func.__name__ 
            if totaltime:
                t1 = time.time()
                result = func(*args, **kwargs)
                t2 = time.time()
                time_Val = int(float( '%.3f' % (t2 - t1))*1000)
                print "%s func end ..." % func.__name__
                print "{1} func total running: {0}ms".format(str(time_Val), func.__name__)
                print "-"*16 + "Response" + "-"*16
                print result
                return result
            else:
                result = func(*args, **kwargs) 
                print "%s func end ..." % func.__name__
                print "-"*16 + "Response" + "-"*16
                print result
                return result
        return _wrapper
    return wrapper


def logger(main):
    #日志配置
    logger = logging.getLogger(main)
    logger.setLevel(level = logging.INFO)
    handler = logging.FileHandler("./log.txt")
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)

    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(formatter)

    logger.addHandler(handler)
    logger.addHandler(console)
    return logger



if __name__ == '__main__':
    logger=logger("mainModule")
    logger.info("mainModule log.")